package cn.ilikes.modules.likes.modules.folders.impls;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.ilikes.modules.likes.dbs.IlikesContent;
import cn.ilikes.modules.likes.dbs.IlikesFolder;
import cn.ilikes.modules.likes.dbs.IlikesUser;
import cn.ilikes.modules.likes.modules.IlikesHbaseService;
import cn.ilikes.modules.likes.modules.folders.IilikesContentService;
import cn.ilikes.modules.likes.modules.folders.IilikesFolderService;
import cn.ilikes.modules.likes.modules.folders.IilikesMylikesException;
import cn.ilikes.modules.likes.modules.users.IUserService;
import cn.ilikes.modules.likes.modules.users.IlikesUserException;

@Component(IilikesContentService.SPRING_SERVICE)
public class IilikesContentServiceImpls implements IilikesContentService {

	@Resource(name = IlikesHbaseService.SPRING_SERVICE)
	private IlikesHbaseService hbaseIlikesService;

	@Resource(name = IUserService.SPRING_SERVICE)
	private IUserService userService;

	@Resource(name = IilikesFolderService.SPRING_SERVICE)
	private IilikesFolderService likesFolderService;

	@Override
	public void addMylikesContent(String userId, String folderId, IlikesContent likesContent) throws IilikesMylikesException {
		try {
			IlikesUser likesUser = userService.getIlikesUser(userId);
			IlikesFolder likesFolder = likesFolderService.getIlikesFolder(folderId);
			likesFolder.getLikesContents().add(likesContent);
			likesContent.setLikesFolder(likesFolder);
			likesContent.setLikesUser(likesUser);
			hbaseIlikesService.save(likesContent);
		} catch (IlikesUserException e) {
			throw new IilikesMylikesException(e);
		}
	}

	@Override
	//select_content_by_folderId
	public List<IlikesContent> listIlikesContentByFolderId(String folderId) throws IilikesMylikesException {
		Map<String, Object> value = new HashMap<String, Object>();
		value.put("folderId", folderId);
		return hbaseIlikesService.findByNameQuery("select_content_by_folderId", value);
	}

	@Override
	public List<IlikesContent> listIlikesContentByContentKey(String contentKey) throws IilikesMylikesException {
		Map<String, Object> value = new HashMap<String, Object>();
		value.put("key", contentKey);
		return hbaseIlikesService.findByNameQuery("select_content_by_key", value);
	}

	@Override
	public IlikesContent get(String key) throws IilikesMylikesException {
		IlikesContent   likesContent = hbaseIlikesService.get(key, IlikesContent.class);
		return likesContent;
	}

	@Override
	public void update(IlikesContent likesContent) throws IilikesMylikesException {
		
		IlikesContent   likesContent_ = hbaseIlikesService.get(likesContent.getUuid(), IlikesContent.class);
		likesContent_.setTitle(likesContent.getTitle());
		likesContent_.setHtml(likesContent.getHtml());
		likesContent_.setContent(likesContent.getContent());
		hbaseIlikesService.update(likesContent_);
		System.out.println("== > "+likesContent_.getLikesUser().getPassword());
		
	}

}
